package cn.doitedu.hebust.pre

import com.alibaba.fastjson.JSON
import org.apache.commons.lang3.StringUtils

import scala.collection.mutable

case class  AppLogBean (
                         eventid :String ,
                         timestamp :Double ,
                         event :Map[String,String] ,
                         uid :String ,
                         phoneNbr :String ,
                         sessionId :String ,
                         imei :String ,
                         mac :String ,
                         imsi :String ,
                         osName :String ,
                         osVer :String ,
                         androidId :String ,
                         resolution :String ,
                         deviceType :String ,
                         deviceId :String ,
                         uuid :String ,
                         appid :String ,
                         appVer :String ,
                         release_ch :String ,
                         promotion_ch :String ,
                         longtitude :Double ,
                         latitude :Double ,
                         carrier :String ,
                         netType :String ,
                         cid_sn :String ,
                         ip :String,
                         var province:String = "",
                         var city:String = "",
                         var district:String = "",
                         var dateStr:String = "",
                         var timeStr:String = ""

                       )
object AppLogBean{

  /**
    * 解析app埋点json日志，返回一个case class
    * @param line
    * @return
    */
  def parseJson2Bean(line:String) :AppLogBean = {
    try {
      val obj = JSON.parseObject(line)

      val eventid = obj.getString("eventid")
      val timestamp = obj.getString("timestamp").toDouble

      val event = obj.getJSONObject("event")
      val eventMap = new mutable.HashMap[String,String]()
      import scala.collection.JavaConversions._
      for(ent <- event.entrySet()){
        eventMap.put(ent.getKey,ent.getValue.toString)
      }

      val user = obj.getJSONObject("user")
      val uid = user.getString("uid")
      val phoneNbr = user.getString("phoneNbr")
      val sessionId = user.getString("sessionId")

      val phone = user.getJSONObject("phone")
      val imei = phone.getString("imei")
      val mac = phone.getString("mac")
      val imsi = phone.getString("imsi")
      val osName = phone.getString("osName")
      val osVer = phone.getString("osVer")
      val androidId = phone.getString("androidId")
      val resolution = phone.getString("resolution")
      val deviceType = phone.getString("deviceType")
      val deviceId = phone.getString("deviceId")
      val uuid = phone.getString("uuid")


      val app = user.getJSONObject("app")
      val appid = app.getString("appid")
      val appVer = app.getString("appVer")
      val release_ch = app.getString("release_ch")
      val promotion_ch = app.getString("promotion_ch")


      val loc = user.getJSONObject("loc")
      val longtitude = loc.getDouble("longtitude")
      val latitude = loc.getDouble("latitude")
      val carrier = loc.getString("carrier")
      val netType = loc.getString("netType")
      val cid_sn = loc.getString("cid_sn")
      val ip = loc.getString("ip")

      AppLogBean(
        eventid ,
        timestamp,
        eventMap.toMap,
        uid ,
        phoneNbr ,
        sessionId ,
        imei ,
        mac ,
        imsi ,
        osName ,
        osVer ,
        androidId ,
        resolution ,
        deviceType ,
        deviceId ,
        uuid ,
        appid ,
        appVer ,
        release_ch ,
        promotion_ch ,
        longtitude ,
        latitude ,
        carrier ,
        netType ,
        cid_sn ,
        ip
      )

    } catch {
      case e: Exception => null
      case _:Throwable => null
    }
  }


  /**
    * 判断一条日志bean是否是有效的
    * @param bean
    * @return
    */
  def isValidBean(bean:AppLogBean):Boolean = {
    val uid = bean.uid
    val imei = bean.imei
    val uuid = bean.uuid
    val mac = bean.mac
    val androidId = bean.androidId
    val ip = bean.ip
    // uid  imei  uuid  mac  androidId  ip  不能全为空
    var flag1 = StringUtils.isNotBlank((uid + imei + uuid + mac + androidId + ip).replaceAll("null",""))

    val event: Map[String, String] = bean.event
    val eventid = bean.eventid
    val sessionId = bean.sessionId
    var flag2 = (event != null) && (StringUtils.isNotBlank(eventid) ) && (StringUtils.isNotBlank(sessionId))

    flag1 && flag2
  }

}